package com.itheima.utils;

import com.itheima.mapper.OnetoOneZj;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class Utils {
    private static SqlSessionFactory sqlSessionFactory;
    private static InputStream is;


    private Utils(){
    }

    static  {
        try {
             is = Resources.getResourceAsStream("mybatisconfig/MyBatisConfig.xml");
             sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
           if (is!=null) {
               try {
                   is.close();
               } catch (IOException e) {
                   e.printStackTrace();
               }
           }
        }
    }


    public static SqlSession getSqlSession() throws IOException {
        return  sqlSessionFactory.openSession(true);
    }
}
